package com.hong.service_product.controller;


import com.hong.fy_common_commomutil.common.result.Result;
import com.hong.fy_model.model.product.ProductCategoryEntity;
import com.hong.service_product.service.ProductCategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 *
 *
 * @author wqh
 * @email 1601709391@qq.com
 * @date 2022-06-15 20:37:30
 */

@Api(tags = "商品类别")
@RestController
@RequestMapping("product/productCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @ApiOperation(value = "新增商品分类接口")
    @PostMapping("/add")
    public Result addProductCategory(@RequestBody ProductCategoryEntity productCategory){
        Result result = productCategoryService.addProductCategory(productCategory);
        return result;
    }

    @ApiOperation(value = "逻辑删除商品分类接口")
    @PostMapping("/delete/{categoryId}")
    public Result logicDeleteProductCategory(@PathVariable Integer categoryId){
        boolean flag = productCategoryService.logicDeleteProductCategory(categoryId);
        if(flag) {
            return Result.ok("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    @ApiOperation(value = "更新商品分类接口")
    @PutMapping("/update")
    public Result update(@RequestBody ProductCategoryEntity productCategory){
        Boolean flag = productCategoryService.updateProductCategory(productCategory);
        if (flag) {
            return Result.ok("更新成功");
        } else {
            return Result.fail("更新失败");
        }
    }

    @ApiOperation(value = "查询分页数据")
    @GetMapping("/listByPage")
    public Result findListByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "limit", defaultValue = "10") Integer limit)
{
        IPage<ProductCategoryEntity> iPage = productCategoryService.getProductCategoryListByPage(pageNum, limit);
        return Result.ok(iPage);
    }

    @ApiOperation(value = "查询所有数据")
    @GetMapping("/list")
    public Result findList(){
        List<ProductCategoryEntity> list = productCategoryService.getProductCategoryList();
        return Result.ok(list);
    }

    @ApiOperation(value = "根据id查询数据")
    @GetMapping("/get/{categoryId}")
    public Result findById(@PathVariable Integer categoryId){
        ProductCategoryEntity productCategory = productCategoryService.getProductCategoryById(categoryId);
        if (productCategory != null) {
            return Result.ok(productCategory);
        } else {
            return Result.fail("未找到该分类");
        }
    }


}
